package cmd

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "Print the version number of math-clc",
	Long:  `All software has versions. This is math-clc's`,
	Run: func(cmd *cobra.Command, args []string) {
		config := viper.AllSettings()
		fmt.Fprintf(os.Stdout, "math-clc author is %s, version is %s\n", config["author"], config["version"])
	},
}

func init() {
	cobra.OnInitialize(initConfig)
	rootCmd.AddCommand(versionCmd)
}

func initConfig() {
	home, err := os.UserHomeDir()
	if err != nil {
		fmt.Fprintf(os.Stdout, err.Error())
		os.Exit(1)
	}

	viper.AddConfigPath(home + "/code/GoPractice/math-clc")
	viper.SetConfigName("config")

	if err = viper.ReadInConfig(); err != nil {
		fmt.Fprintf(os.Stdout, "Can't read config:%s", err.Error())
		os.Exit(1)
	}
}
